<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :inline="true">
      <el-form-item style="width: 150px">
        <el-input v-model="searchMap.data.homeName" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item style="width: 150px">
        <el-input
          v-model="searchMap.data.username"
          placeholder="归属用户"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="searchBtn()"
          >查询</el-button
        >
        <el-button
          icon="el-icon-delete"
          type="danger"
          @click="delHouses()"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 民宿信息列表表格 -->
    <el-table :data="houseList" style="width: 100%" ref="houseTable">
      <el-table-column prop="homeName" label="民宿名称" width="150px"></el-table-column>
      <el-table-column prop="merchantId" label="归属用户"> </el-table-column>
      <el-table-column prop="homeDesc" label="民宿描述" show-overflow-tooltip></el-table-column>
      <el-table-column prop="" label="民宿图片详解">
        <template slot-scope="scope">
          <div >
            <img style="height:80px" :src="scope.row.homePic" alt="">
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="homeRemark" label="备注"></el-table-column>
      <el-table-column label="操作" width="130">
        <template slot-scope="scope">
          <!-- 自定义展示 -->
          <div>
            <el-tooltip content="修改民宿信息" placement="top" :enterable="false">
              <el-button type="warning" size="mini" icon="el-icon-edit" circle :value="scope.row" @click="updateHouse(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除民宿信息" placement="top" :enterable="false">
              <el-button type="danger" size="mini" icon="el-icon-delete" circle :value="scope.row" @click="delHouse(scope.row)"></el-button>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页栏 -->
    <el-pagination
      style="text-align: center"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchMap.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
     <!-- 民宿信息弹出层 编号2-->
    <el-dialog
        :title="houseEntity.homeId?'修改民宿信息':'添加顾民宿'"
        :visible.sync="dialogFormVisible2"
        :close-on-click-modal="false"
        >
        <el-form :model="houseEntity"  ref="houserForm" :rules="houseRules">
            <el-form-item label="民宿名称" label-width="100px" ><!-- prop="houseName" -->
            <el-input v-model="houseEntity.homeName"  style="color:black" placeholder="民宿名称 必填！！"></el-input>
            </el-form-item>
            <el-form-item label="归属用户" label-width="100px">
            <el-input v-model="houseEntity.merchantId" style="color:black"></el-input>
            </el-form-item>
            <el-form-item label="民宿描述"  label-width="100px">
            <el-input v-model="houseEntity.homeDesc" style="color:black"></el-input>
            </el-form-item>
            <el-form-item label="图片展示"  label-width="100px">
            <el-input v-model="houseEntity.homePic" style="color:black"></el-input>
            </el-form-item>
            <el-form-item label="备注"  label-width="100px">
            <el-input v-model="houseEntity.homeRemark" style="color:black"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible2 = false">取 消</el-button>
            <el-button type="primary" @click="submithouserForm('houserForm')">确 定</el-button>
        </div>
        </el-dialog>

  </div>
</template>


<script>
import { listSearch,houseSave,delById,delByIds } from "@/api/houselist.js";
  var validHouseName=(rule,value,callback)=>{
    if(!value){
      callback(new Error('请输入民宿名称'))
    }else{
      callback()
    }
  }
export default {
  created() {
    this.search();
  },
  data() {
    return {
      searchMap: {
        page: 1,
        limit: 10,
        data: {},
      },
      houseList: [],
      dialogFormVisible1: false,
      dialogFormVisible2:false,
      dialogFormVisible3:false,
      total: 0,
      //民宿表单校验
      houseRules: {
        houseName:[
          { required: true, message: '请输入民宿名称', trigger: 'blur' }
        ]
        // ,
        // phone:[
        //   { required: true, message: '请输入民宿电话号码', trigger: 'blur' },
        //   { required: true, trigger: 'blur', validator: validPhone }
        // ]
      },
      //民宿信息表单校验
      userinfoRules: {
        price:[
          { pattern: /^-?\d+\.?\d*$/, message: '请输入纯数字', trigger: 'blur'}
        ]
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      userInfoList:[],
      priceTotal:0,
      userinfoEntity: {},
      houseEntity:{},
      userId:0,
    };
  },
  methods: {
    searchBtn(){
      this.searchMap.page=1;
      this.search();
    },
    search() {
      listSearch(this.searchMap).then((res) => {
        this.houseList = res.data;
        this.total = res.total;
      });
    },
    handleSizeChange(pageSize) {
      //改变每页数据显示条数触发
      this.searchMap.limit = pageSize;
      this.search();
    },
    handleCurrentChange(currentPage) {
      //查看指定页数事件触发
      this.searchMap.page = currentPage;
      this.search();
    },

    //添加民宿信息按钮触发事件
    addHouse(){
      this.dialogFormVisible2 = true;
      this.houseEntity = {};
    },
    //修改民宿信息按钮触发事件
    updateHouse(row){
      this.dialogFormVisible2 = true;
      this.houseEntity = JSON.parse(JSON.stringify(row));
    },
    //添加保存与修改保存
    saveHouse(){
      console.log(this.houseEntity)
      houseSave(this.houseEntity).then(res=>{
          this.dialogFormVisible2 = false;
          this.$message({
            message: res.message,
            type: res.success,
            center: true,
          });
          this.search();
      });
    },

    //民宿信息行删除
    delHouse(row){
      this.$confirm("此操作将删除该行记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          delById(row.homeId).then((res) => {
          this.$message({
                message: res.message,
                type: res.success,
                center: true,
          });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //民宿信息批量删除
    delHouses(){
      var rowDatas = this.$refs.houseTable.selection;
      var ids=[];
      
      this.$confirm("此操作将删除选中的多条记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          rowDatas.forEach(element => {
             ids.push(element.homeId);
          });
          console.log(ids)
          delByIds(ids).then((res) => {
          this.$message({
            message: res.message,
            type: res.success,
            center: true,
          });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //民宿信息表单submit按钮
    submithouserForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.saveHouse();
          } else {
            this.$message({
                type: "error",
                message: "请按要求填写民宿信息！！",
              });
            return false;
          }
        });
    },
    //民宿信息表单submit按钮
    submitUserinfoForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.saveUserInfo();
          } else {
            this.$message({
                type: "error",
                message: "请按要求填写民宿配镜信息！！",
              });
            return false;
          }
        });
    },
   
    

    formatTime(row, column) {
      let updateTime = new Date(row[column.property]);
      return (
        updateTime.getFullYear() +
        "年" +
        updateTime.getMonth() +
        "月" +
        updateTime.getDate() +
        "日 " +
        updateTime.getHours() +
        ":" +
        updateTime.getMinutes()
      );
    },
  },
};
</script>
<style lang="scss">
.goodsStatus {
  background-color: rgb(51, 171, 160);
}
</style>